<?php
if (init()) {
  redirect("?action=index");
  exit();
}

$initiatorAction = \controller\ControllerUtil::getEncodedActionFromUrlWithParameters($_SERVER['HTTP_REFERER']);

$fname = trim($_POST['fname']);
$lname = trim($_POST['lname']);
$login = trim($_POST['login']);
$email = trim($_POST['email']);
$mobile = trim($_POST['mobile']);
$birth = trim($_POST['birth']);
$agree = trim($_POST['agree']);
$work = trim($_POST['work']);
$methods = trim($_POST['methods']);
$address = trim($_POST['address']);
$icq = trim($_POST['icq']);
$skype = trim($_POST['skype']);


if ($fname == ''
    or
    $lname == ''
    or
    $login == ''
    or
    $email == ''
    or
    $mobile == ''
) {
  redirect("?action=msg&header=header.registration.fields.empty&body=body.registration.fields.empty&view=" . $initiatorAction);
} else {
  $fakePass = generatePassword(8);
  $pass = encryptString($login, $fakePass);
}

$checkNickResultSet = checkNickOrEmail(trim($_POST['login']), trim($_POST['email']));

if (mysql_fetch_assoc($checkNickResultSet)) {
  redirect("?action=msg&header=header.registration.user.exists&body=body.registration.user.exists&view=" . $initiatorAction);
}

$timeStamp = getLocalTimeStamp();

$insertUserResultSet = createNewUser($fname, $lname, $login, f_Date($timeStamp), $email, $icq, $skype, $mobile, $address, $work, $methods, $pass, $birth);

$groupInsertResultSet = insertNewUserIntoBaseGroup($login);

if ($insertUserResultSet && $groupInsertResultSet) {
  $to = $email;
  $subject = getMessageByKey('email.registration.subject');
//  $message = "<html>
//<head>
//  <title>Добро пожаловать !</title>
//</head>
//<body>
//  <p>Рады приветствовать Вас на форуме TheFlyNet.com. Вы указали этот электронный адрес при регистрации. Ваши регистрационные данные: </p>
//  <table>
//    <tr>
//      <td>Логин:</td><td> " . $login . "</td>
//    </tr>
//    <tr>
//      <td>Пароль:</td><td>" . $fakePass . "</td>
//    </tr>
//  </table>
//</body>
//</html>";

  $params['login'] = $login;
  $params['pass'] = $fakePass;
  $message = getMessageByKeyWithParam('email.registration.body', $params);

  $headers = "From: administration@theflynet.com" . "\r\n";
  $headers .= 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";;
  mail($to, "=?utf-8?B?".base64_encode($subject)."?=", $message, $headers);
  redirect("?action=msg&header=header.registration.success&body=body.registration.success&view=index");
} else {
  redirect("?action=msg&header=header.registration.failed&body=body.registration.failed&view=" . $initiatorAction);
}





